<?php
/**
 * Intervention
 *
 *** @package Pharmacy Intervention
 * @subpackage Controller
 * @category Intervention
 * @author Ray Nowell */
class Intervention extends MY_Controller {

	function Intervention()
	{
		parent::MY_Controller();
	}

	//Private Functions:
	function _get_form_post_data($add_record = false)
	{
		$this->load->helper('date');
		$this->load->model('Intervention_model');	

		//Make date mysql friendly
		$date_var = explode('/',$this->input->post('intervention_date'));

		$intervention_id = $this->input->post('intervention_id');
		$patient_id_number = strtoupper($this->input->post('patient_id_number'));
		$patient_id = strtoupper($this->input->post('patient_id'));
		$intervention_date = $date_var[2].'-'.$date_var[0].'-'.$date_var[1];
		$intervention_code_id = $this->input->post('intervention_code_id');
		$comment = $this->input->post('comment');
		$status = $this->input->post('status');
		$physician_id = $this->input->post('physician_id');
		$unit_patient_location_id = $this->input->post('unit_patient_location_id');
		$pharmacist_id = $this->input->post('pharmacist_id');
		$ordered_drug_id = $this->input->post('ordered_drug_id');
		$ordered_drug_text = $this->input->post('ordered_drug_text');
		$ordered_unit_cost = str_replace(',','',str_replace('$','',$this->input->post('ordered_unit_cost')));
		$ordered_drug_frequency_id = $this->input->post('ordered_drug_frequency_id');
		$ordered_doses_per_day = $this->input->post('ordered_doses_per_day');

		if($this->Intervention_model->intervention_is_risk_mitigation($this->input->post('intervention_code_id')))
		{
			$recommended_drug_id = null;
			$recommended_drug_text = null;
			$recommended_unit_cost = null;
			$recommended_drug_frequency_id = null;
			$recommended_doses_per_day = null;
			$days_of_tx = null;
			$current_tx_day = null;
		}
		else
		{
			$recommended_drug_id = $this->input->post('recommended_drug_id');
			$recommended_drug_text = $this->input->post('recommended_drug_text');
			$recommended_unit_cost = str_replace(',','',str_replace('$','',$this->input->post('recommended_unit_cost')));
			$recommended_drug_frequency_id = $this->input->post('recommended_drug_frequency_id');
			$recommended_doses_per_day = $this->input->post('recommended_doses_per_day');
			$days_of_tx = $this->input->post('days_of_tx');
			$current_tx_day = $this->input->post('current_tx_day');
		}

		$data = array(
					'intervention_id' 				=>	$intervention_id,
					'patient_id_number' 			=>	$patient_id_number,
					'patient_id'		 			=>	$patient_id,
					'intervention_date' 			=>	$intervention_date,
					'intervention_code_id'			=>	$intervention_code_id,
					'comment'						=>	$comment,
					'status'						=>	$status,
					'physician_id'					=>	$physician_id,
					'unit_patient_location_id'		=>	$unit_patient_location_id,
					'pharmacist_id'					=>	$pharmacist_id,
					'ordered_drug_id'				=>	$ordered_drug_id,
					'ordered_drug_text'				=>	$ordered_drug_text,
					'ordered_unit_cost'				=>	$ordered_unit_cost,
					'ordered_drug_frequency_id'		=>	$ordered_drug_frequency_id,
					'ordered_doses_per_day'			=>	$ordered_doses_per_day,
					'recommended_drug_id'			=>	$recommended_drug_id,
					'recommended_drug_text'			=>	$recommended_drug_text,
					'recommended_unit_cost'			=>	$recommended_unit_cost,
					'recommended_drug_frequency_id'	=>	$recommended_drug_frequency_id,
					'recommended_doses_per_day'		=>	$recommended_doses_per_day,
					'days_of_tx'					=>	$days_of_tx,
					'current_tx_day'				=>	$current_tx_day,
					'facility_id'					=>	$this->current_facility_id,
					'modified_date'					=>	date("Y-m-d H:i:s"),
					'modified_by'					=>	$this->current_user,
		);
    
    if ($add_record)
      array_shift($data);

		return $data;
	}

	/**
	 * Called from the Clinical Intervention List Add Button, this function loads
	 * empty default values into the clinical intervention form and then loads the
	 * intervention view
	 *
	 * @access public */
	function add()
	{
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model('Intervention_model');

		$data['title'] = COMPANY_NAME." -- Add Intervention ";
		$data['heading'] = "Clinical Intervention Form";

		//Set the values for the Intervention View
		$data['intervention_id']	=	'';
		$data['patient_id_number']	=	'';
		$data['patient_id']			=	'';

		$date_string							=	'm/d/Y';
		$data['intervention_date']				=	date($date_string);//mdate($date_string, mysql_to_unix($intervention_row->intervention_date));
		$data['intervention_code_id']			=	'-1';
		$data['comment']						=	'';
		$data['status']							=	'';
		$data['physician_id']					=	'-1';
		$data['unit_patient_location_id']		=	'-1';
		$data['pharmacist_id']					=	'-1';
		$data['ordered_drug_id']				=	'';
		$data['ordered_drug_text']				=	'';
		$data['ordered_unit_cost']				=	'';
		$data['ordered_drug_frequency_id']		=	'-1';
		$data['ordered_doses_per_day']			=	'';
		$data['recommended_drug_id']			=	'';
		$data['recommended_drug_text']			=	'';
		$data['recommended_unit_cost']			=	'';
		$data['recommended_drug_frequency_id']	=	'-1';
		$data['recommended_doses_per_day']		=	'';
		$data['days_of_tx']						=	'';
		$data['current_tx_day']					=	'';
		$data['days_remaining']					=	'';
		$data['created_date']					=	'';
		$data['modified_date']					=	'';
		

		$data['save_type'] 			= 'save_new';
		$data['user_facility_name'] = $this->current_facility_name;



		////////////////////////
		//Store look-up values//
		////////////////////////
		$intervention_code_lookup	=	$this->Intervention_model->get_intervention_code_lookup();
		$data['my_code_list']		=	set_lookup($intervention_code_lookup,'intervention_code_id','code',BLANK_ROW);
		$data['my_code_list_box']	=	set_lookup($intervention_code_lookup,'intervention_code_id','code');

		$physician_lookup		=	$this->Intervention_model->get_physician_lookup();
		$data['physician_list']	=	set_lookup($physician_lookup,'physician_id','full_name',BLANK_ROW);

		$unit_patient_location_lookup		=	$this->Intervention_model->get_unit_patient_location_lookup();
		$data['unit_patient_location_list']	=	set_lookup($unit_patient_location_lookup,'unit_patient_location_id','unit_patient_location',BLANK_ROW);

		$pharmacist_lookup			=	$this->Intervention_model->get_pharmacist_lookup();
		$data['pharmacist_list']	=	set_lookup($pharmacist_lookup,'pharmacist_id','full_name',BLANK_ROW);

		$drug_frequency_lookup			=	$this->Intervention_model->get_drug_frequency_lookup();
		$data['drug_frequency_list']	=	set_lookup($drug_frequency_lookup,'drug_frequency_id','frequency',BLANK_ROW);
		$data['drug_frequency_ratio']	=	set_lookup($drug_frequency_lookup,'drug_frequency_id','frequency_ratio'); //used in javascript to calculate savings

		$data['status_list']	=	$this->Intervention_model->get_status_lookup(); //Does not need to by set like previous lists because it is a hard-coded array

		$this->load->view('intervention_view', $data);

	}

	/**
	 * Used for editing interventions, this form loads the values of an existing
	 * intervention and passes those values onto the Clinical Intervention view
	 *
	 * If this form is loaded as SINGLE_EDIT (e.g. indicated by the total number of segments in the url, then no navigation bar will display)
	 * Otherwise, the user has selected multiple interventions to edit and the navigation bar must be loaded.
	 *
	 * @access public */
	function edit()
	{
		//$this->output->enable_profiler(TRUE);
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model('Intervention_model');

		$data['in_add_mode'] = false; //Currently determines if Clear Form Button should be shown

		$data['title'] = COMPANY_NAME." -- Intervention Edit";
		$data['heading'] = "Intervention Edit";

		//The number of segments will determine the edit mode of the intervention:single-list or multi-record navigation
		$total_segments = $this->uri->total_segments();

		//See if editing a single intervention or multiple interventions
		if ($total_segments == SINGLE_EDIT)
		{
			//Single intervention:  only load the basics for this form
			$ci_id = $this->uri->segment(3, 0);//Three is the Intervention Id.  zero is the default value
		}
		else
		{
			//Multiple interventions selected to edit.
			//Segment 3 is used to determine which intervention id should be loaded
			//Also prep the javascript to determine which segement should be loaded next
			//based on what the user selects (e.g. next or previous)
				
			$segment_to_get = $this->uri->segment(3);

			$ci_id = $this->uri->segment($segment_to_get);

			$previous_segment = $segment_to_get <= 4 ? 4 : $segment_to_get - 1;
			$next_segment = $segment_to_get == $total_segments  ? $total_segments : $segment_to_get + 1;
			$fullUri = $this->uri->uri_string();
			$search_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
			$replace_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'|';
			$current_uri = str_replace($search_text, $replace_text, $fullUri);
			$nav_url = base_url().'index.php'.$current_uri;
			$data['additional_javascript_doc_ready_functions'] = get_navigation_doc_ready_javascript();
			$data['additional_jquery_javascript'] = get_navigation_functions($nav_url, $previous_segment, $next_segment, $total_segments);
			$data['nav_bar'] = get_navigation_markup(base_url(), $segment_to_get - 3, $total_segments - 3);
		}

		$intervention_row = $this->Intervention_model->get_intervention_record($ci_id);

		//Set the values for the Intervention View
		$data['intervention_id']	=	$intervention_row->intervention_id;
		$data['patient_id_number'] 	=	$intervention_row->patient_id_number;
		$data['patient_id']			=	$intervention_row->patient_id;

		$date_string = '%m/%d/%Y';
		$data['intervention_date'] = mdate($date_string, mysql_to_unix($intervention_row->intervention_date));
		$data['intervention_code_id'] = $intervention_row->intervention_code_id;
		$data['comment'] = $intervention_row->comment;
		$data['status'] = $intervention_row->status;
		$data['physician_id'] = $intervention_row->physician_id;
		$data['unit_patient_location_id'] = $intervention_row->unit_patient_location_id;
		$data['pharmacist_id'] = $intervention_row->pharmacist_id;
		$data['ordered_drug_id'] = $intervention_row->ordered_drug_id;
		$data['ordered_drug_text'] = $intervention_row->ordered_drug_text;
		$data['ordered_unit_cost'] = $intervention_row->ordered_unit_cost;
		$data['ordered_drug_frequency_id'] = $intervention_row->ordered_drug_frequency_id;
		$data['ordered_doses_per_day'] = $intervention_row->ordered_doses_per_day;
		$data['recommended_drug_id'] = $intervention_row->recommended_drug_id;
		$data['recommended_drug_text'] = $intervention_row->recommended_drug_text;
		$data['recommended_unit_cost'] = $intervention_row->recommended_unit_cost;
		$data['recommended_drug_frequency_id'] = $intervention_row->recommended_drug_frequency_id;
		$data['recommended_doses_per_day'] = $intervention_row->recommended_doses_per_day;
		$data['days_of_tx'] = $intervention_row->days_of_tx;
		$data['current_tx_day'] = $intervention_row->current_tx_day;
		$data['created_date'] = $intervention_row->created_date;
		$data['modified_date'] = $intervention_row->modified_date;
		
		$data['user_facility_name'] = $this->current_facility_name;

		$data['save_type'] = 'update';

		////////////////////////
		//Get look-up values//
		////////////////////////
		$intervention_code_lookup = $this->Intervention_model->get_intervention_code_lookup();
		$data['my_code_list'] = set_lookup($intervention_code_lookup,'intervention_code_id','code');
		$data['my_code_list_box'] = set_lookup($intervention_code_lookup,'intervention_code_id','code');

		$physician_lookup = $this->Intervention_model->get_physician_lookup($intervention_row->physician_id);
		$data['physician_list'] = set_lookup($physician_lookup,'physician_id','full_name');

		$unit_patient_location_lookup = $this->Intervention_model->get_unit_patient_location_lookup($intervention_row->unit_patient_location_id);
		$data['unit_patient_location_list'] = set_lookup($unit_patient_location_lookup,'unit_patient_location_id','unit_patient_location');

		$pharmacist_lookup = $this->Intervention_model->get_pharmacist_lookup($intervention_row->pharmacist_id);
		$data['pharmacist_list'] = set_lookup($pharmacist_lookup,'pharmacist_id','full_name');

		$drug_frequency_lookup = $this->Intervention_model->get_drug_frequency_lookup();
		$data['drug_frequency_list'] = set_lookup($drug_frequency_lookup,'drug_frequency_id','frequency');
		$data['drug_frequency_ratio'] = set_lookup($drug_frequency_lookup,'drug_frequency_id','frequency_ratio'); //used in javascript to calculate savings

		$data['status_list'] = $this->Intervention_model->get_status_lookup(); //Does not need to by set like previous lists because it is a hard-coded array

		$this->load->view('intervention_view', $data);
	}
	
	function copy()
	{
		$data['title']		=	'Copy Intervention';
		$data['heading']	=	'Copy Intervention';
		
		$data['month_list']	=	array('01'=>1,'02'=>2,'03'=>3,'04'=>4,'05'=>5,'06'=>6,'07'=>7,'08'=>8,'09'=>9,'10'=>10,'11'=>11,'12'=>12);
		
		$data['opt_copy_to_next_month'] = array(	'name'		=>	'date_action',
												'id'		=>	'_optCopyToNextMonth',
												'value'		=>	'Copy to Next Month',
												'selected'	=>	TRUE,
												);
										
		$data['opt_specify_date']	=	array(	'name'		=>	'date_action',
												'id'		=>	'_optSpecifyDate',
												'value'		=>	'Specify Date',
												'selected'	=>	TRUE,
											);
				
		$data['user_facility_name']	=	$this->current_facility_name;
		
		$copy_ids	=	$this->uri->uri_string();
		$data['copy_ids']	=	str_replace('intervention/copy/','',$copy_ids);
		
		$this->load->view('intervention_copy_view', $data);
		
	}


	/**
	 * Calls the Intervention Model to save the intervention based on the post data passed
	 * @access public */
	function save_new()
	{
		//$this->output->enable_profiler(TRUE);
		$data = $this->_get_form_post_data(true);

		$data['created_date'] = date('Y-m-d H:i:s');
		$data['created_by'] = $this->ion_auth->get_user()->username;

		$this->Intervention_model->Add_intervention($data);
		redirect('intervention/add');
	}

	/**
	 * Calls the Intervention Model to update the intervention based on the post data passed
	 * @access public */
	function update()
	{
		//$this->output->enable_profiler(TRUE);
		$uri	=	$this->input->post('uri');
		$data = $this->_get_form_post_data();
		$this->Intervention_model->Update_intervention($data);
		redirect($uri);
	}
}

/* End of file intervention.php */
/* Location: ./system/application/controllers/intervention.php */
